package com.bennu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bennu.Dto.SetmealDto;
import com.bennu.common.R;
import com.bennu.domain.Category;
import com.bennu.domain.Setmeal;
import com.bennu.service.CategoryService;
import com.bennu.service.SetmealDishService;
import com.bennu.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    //添加套餐
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info( setmealDto.toString() );
        setmealService.saveWithDish( setmealDto );
        return R.success( "新增套餐成功" );
    }
    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page ,int pageSize,String name){
        //构造分页构造器
        Page<Setmeal> pageInfo=new Page<>();
        Page<SetmealDto> setmealDtoPage=new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件
        queryWrapper.like( name!=null,Setmeal::getName,name );
        //添加排序条件
        queryWrapper.orderByDesc( Setmeal::getUpdateTime );
        setmealService.page( pageInfo,queryWrapper );
        //将pageInfo查询到的结果拷贝到setmealDtoPage中(忽略records)
        //records是分页中的所有数据（泛型也不同）
        BeanUtils.copyProperties( pageInfo,setmealDtoPage,"records" );
        List<Setmeal> records = pageInfo.getRecords();
        //List<Setmeal>转换为List<SetmealDto>
        List<SetmealDto> list=records.stream().map( (item)->{
            SetmealDto setmealDto=new SetmealDto();
            //在setmealDto中只对setCategoryName赋了值，将item其他属性复制到setmealDto
            BeanUtils.copyProperties( item,setmealDto );
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查到分类对象
            Category category = categoryService.getById( categoryId );
            if ( category!=null ){
                //获取分类名称
                String categoryName = category.getName();
                //将分类名称赋给SetmealDto
                setmealDto.setCategoryName( categoryName );
            }

            return setmealDto;
        } ).collect( Collectors.toList() );
        setmealDtoPage.setRecords( list );
        return R.success( setmealDtoPage );
    }
    //删除套餐
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        log.info( ids.toString() );
        setmealService.removeWithDish( ids );
        return R.success( "删除成功" );
    }
    //套餐起售状态
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,Long [] ids ){
        log.info( status.toString(),ids);
        for (int i = 0; i < ids.length; i++) {
            Long id = ids[i];
            Setmeal byId = setmealService.getById( id );
            byId.setStatus( status );
            setmealService.updateById( byId );
        }
        return R.success( "修改成功" );
    }
    //修改套餐根据id查询
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id){
        log.info( id.toString());
        SetmealDto setmealDto = setmealService.getByIdWithDish( id );
        return R.success( setmealDto );
    }
    //修改
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        log.info( setmealDto.toString() );
        setmealService.updateWithDish(setmealDto);
        return R.success( "菜品修改成功" );
    }
    //用户端套餐展示
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> list( Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq( setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId() );
        queryWrapper.eq( setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus() );
        queryWrapper.orderByDesc( Setmeal::getUpdateTime );
        List<Setmeal> list = setmealService.list( queryWrapper );

        return R.success( list );
    }
    //套餐详情
    @GetMapping("/dish/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        log.info( id.toString());
        SetmealDto setmealDto = setmealService.getByIdWithDish( id );
        return R.success( setmealDto );
    }

}
